package pers.cyril.study.graphql.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import pers.cyril.study.graphql.demo.entity.Area;
import pers.cyril.study.graphql.demo.entity.City;
import pers.cyril.study.graphql.demo.entity.Province;
import pers.cyril.study.graphql.demo.service.AddressService;

import java.awt.print.Book;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说点什么吧
 *
 * @author Wang Cyril
 * @date 2024/7/2 21:46
 */
@Controller
public class GraphQLController {

    @Autowired
    private AddressService addressService;

    //等同于 @SchemaMapping(typeName="Query", field="provinces")
    @QueryMapping
    public List<Province> provinces() {
        return addressService.getProvinces();
    }

    @QueryMapping
    public Province province(@Argument String code) {
        return addressService.getProvince(code);
    }

    @QueryMapping
    public City city(@Argument String code) {
        return addressService.getCity(code);
    }

    @QueryMapping
    public Area area(@Argument String code) {
        return addressService.getArea(code);
    }

    @BatchMapping(typeName = "Province",field = "cities")
    public Map<Province, List<City>> getCities(List<Province> provinces) {
        System.out.println("Fetching cities for all Province");
        Map<Province, List<City>> result = new HashMap<>();
        for (Province province : provinces) {
            result.put(province,addressService.getCitiesByPCode(province.getCode()));
        }
        return result;
    }

    @BatchMapping(typeName = "City",field = "areas")
    public Map<City, List<Area>> getAreas(List<City> cities) {
        System.out.println("Fetching areas for all City");
        Map<City, List<Area>> result = new HashMap<>();
        for (City city : cities) {
            result.put(city,addressService.getAreasByPcode(city.getCode()));
        }
        return result;
    }

//    @SchemaMapping(typeName = "Province", field = "cities")
//    public List<City> getCities(Province province) {
//        return addressService.getCitiesByPCode(province.getCode());
//    }
//
//    @SchemaMapping(typeName = "City", field = "areas")
//    public List<Area> getAreas(City city) {
//        return addressService.getAreasByPcode(city.getCode());
//    }


}
